<?php
class TestArrayController extends AppController{
	public function index(){
		$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
		
		function test_alter(&$item1, $key, $prefix)
		{
		    $item1 = "$prefix: $item1";
		}
		
		function test_print($item2, $key)
		{
		    echo "$key. $item2<br />\n";
		}
		
		echo "Before ...:\n";
		array_walk($fruits, 'test_print');
		
		array_walk($fruits, 'test_alter', 'fruit');
		echo "... and after:\n";
		
		array_walk($fruits, 'test_print');
	}
	
	private function addtext($test){
		return $test." test";
	}
	
	public function test(){
		$list = array();
		for($i=0; $i<10000; $i++){
			array_push($list, rand(0, 10000));
		}
		function add(&$item, $key, $text){
			$item = $item.$text;
		}
		$func = function ($value){
			return $value." test";
		};
		
		$start = microtime(true);
		foreach ($list as $key => $item){
			$item = $this->addtext($item);
		}
		print_r(microtime(true) - $start);
		echo "<br/>";
		
		$start = microtime(true);
		array_walk($list, 'add', 'test');
		print_r(microtime(true) - $start);
		echo "<br/>";
		$start = microtime(true);
		array_map($func, $list);
		print_r(microtime(true) - $start);
		exit;
	}
}